package com.bkhc.business.repository;

import com.alibaba.fastjson.JSONArray;
import com.bkhc.business.entity.BusinessPriceAccount;
import com.bkhc.business.entity.vo.Order;
import com.bkhc.common.to.ProductPriceTo;
import com.bkhc.common.util.response.PageUtil;
import com.bkhc.common.util.response.ResultJson;
import com.mysql.cj.xdevapi.JsonArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ProductRepository {
    @Autowired
    private RestTemplate restTemplate;

    public List<ProductPriceTo> getGoodsPrice(Map<String,String> paramMap , int pageIndex, int pageSize) {
        String serviceName = "PRODUCT-APP";
        String url = "http://" + serviceName + "/service/price/queryProductPrice?pageNum=" + pageIndex + "&pageSize=" + pageSize;
        ResultJson<List<ProductPriceTo>> resultJson = restTemplate.postForObject(url, paramMap, ResultJson.class);
        return resultJson.getData();
    }

    public PageUtil getOrder(Order order, int pageIndex, int pageSize) {
        String serviceName = "ORDER-APP";
        String url = "http://" + serviceName + "/service/oder/list?pageNum=" + pageIndex + "&pageSize=" + pageSize;
        ResultJson<PageUtil> result = restTemplate.postForObject(url, order, ResultJson.class);
        PageUtil pageUtil = result.getData();
        return pageUtil;
    }

    public ResultJson addProductor(String userId,String productCode) {
        String serviceName = "PRODUCT-APP";
        String url = "http://" + serviceName + "/productor/add" ;
        Map<String,String> paramMap = new HashMap<>();
        paramMap.put("userId",userId);
        paramMap.put("productCode",productCode);
        ResultJson result = restTemplate.postForObject(url,paramMap,ResultJson.class);
        return result;
    }

    public List getSpecsByGoodsList(List<String> goodsList) {
        String serviceName = "PRODUCT-APP";
        String url = "http://" + serviceName + "/product/goods/getSpecsByGoodsList" ;
        List result = restTemplate.postForObject(url,goodsList,List.class);

        return result;
    }


    public Collection<? extends ProductPriceTo> getGoodsPriceByPriceCount(List<BusinessPriceAccount> accountList) {
        String serviceName = "PRODUCT-APP";
        String url = "http://" + serviceName + "/service/price/queryProductPriceByPriceAccount";
        ResultJson<List<ProductPriceTo>> resultJson = restTemplate.postForObject(url, accountList, ResultJson.class);
        return resultJson.getData();
    }
}
